<script setup>
import { Head, Link, useForm } from '@inertiajs/vue3'
import Layout from '@/Layouts/Layout.vue'
import TextInput from '@/Components/TextInputPing.vue'
import SelectInput from '@/Components/SelectInput.vue'
import LoadingButton from '@/Components/LoadingButton.vue'
import TrashedMessage from '@/Components/TrashedMessage.vue'

const props = defineProps({
    organization: Array,
})

const form = useForm({
    name: props.organization.name,
    email: props.organization.email,
    phone: props.organization.phone,
    address: props.organization.address,
    city: props.organization.city,
    region: props.organization.region,
    country: props.organization.country,
    postal_code: props.organization.postal_code,
})

function update() {
    form.put(`/organizations/${props.organization.id}`)
}

function destroy() {
    if (confirm('Are you sure you want to delete this organization?')) {
        form.delete(`/organizations/${props.organization.id}`)
    }
}

function restore() {
    if (confirm('Are you sure you want to restore this organization?')) {
        form.put(`/organizations/${props.organization.id}/restore`)
    }
}
</script>

<template>
    <Layout>
        <div>
            <Head :title="form.name" />
            <div class="flex justify-start mb-8 max-w-3xl">
                <h1 class="text-3xl font-bold">
                    <Link class="text-indigo-400 hover:text-indigo-600" :href="route('organizations.index')">Organizations</Link>
                    <span class="text-indigo-400 font-medium">/</span>
                    {{ form.name }}
                </h1>
            </div>
            <TrashedMessage v-if="organization.deleted_at" class="mb-6" @restore="restore"> This organization has been deleted. </TrashedMessage>
            <div class="max-w-3xl bg-white rounded-md shadow overflow-hidden">
                <form @submit.prevent="update">
                    <div class="flex flex-wrap -mb-8 -mr-6 p-8">
                        <TextInput v-model="form.name" :error="form.errors.name" class="pb-8 pr-6 w-full lg:w-1/2" label="Name" />
                        <TextInput v-model="form.email" :error="form.errors.email" class="pb-8 pr-6 w-full lg:w-1/2" label="Email" />
                        <TextInput v-model="form.phone" :error="form.errors.phone" class="pb-8 pr-6 w-full lg:w-1/2" label="Phone" />
                        <TextInput v-model="form.address" :error="form.errors.address" class="pb-8 pr-6 w-full lg:w-1/2" label="Address" />
                        <TextInput v-model="form.city" :error="form.errors.city" class="pb-8 pr-6 w-full lg:w-1/2" label="City" />
                        <TextInput v-model="form.region" :error="form.errors.region" class="pb-8 pr-6 w-full lg:w-1/2" label="Region" />
                        <SelectInput v-model="form.country" :error="form.errors.country" class="pb-8 pr-6 w-full lg:w-1/2" label="Country">
                            <option :value="null" />
                            <option value="CA">Canada</option>
                            <option value="US">United States</option>
                        </SelectInput>
                        <TextInput v-model="form.postal_code" :error="form.errors.postal_code" class="pb-8 pr-6 w-full lg:w-1/2" label="Postal code" />

                    </div>
                    <div class="flex items-center px-8 py-4 bg-gray-50 border-t border-gray-100">
                        <button v-if="!organization.deleted_at" class="text-red-600 hover:underline" tabindex="-1" type="button" @click="destroy">Delete Organization</button>
                        <LoadingButton :loading="form.processing" class="btn-indigo ml-auto" type="submit">Update Organization</LoadingButton>
                    </div>
                </form>
            </div>
            <h2 class="mt-12 text-2xl font-bold">Contacts</h2>
            <div class="mt-6 bg-white rounded shadow overflow-x-auto">
                <table class="w-full whitespace-nowrap">
                    <tr class="text-left font-bold">
                        <th class="pb-4 pt-6 px-6">Name</th>
                        <th class="pb-4 pt-6 px-6">City</th>
                        <th class="pb-4 pt-6 px-6">Phone</th>
                    </tr>
                    <tr v-for="contact in organization.contacts" :key="contact.id" class="hover:bg-gray-100 focus-within:bg-gray-100">
                        <td class="border-t">
                            <Link class="flex items-center px-6 py-4 focus:text-indigo-500" :href="route('contacts.edit', contact.id)">
                                {{ contact.name }}
                                <Icon v-if="contact.deleted_at" name="trash" class="flex-shrink-0 ml-2 w-3 h-3 fill-gray-400" />

                            </Link>
                        </td>
                        <td class="border-t">
                            <Link class="flex items-center px-6 py-4" :href="route('contacts.edit', contact.id)" tabindex="-1">
                                {{ contact.city }}
                            </Link>
                        </td>
                        <td class="border-t">
                            <Link class="flex items-center px-6 py-4" :href="route('contacts.edit', contact.id)" tabindex="-1">
                                {{ contact.phone }}
                            </Link>
                        </td>
                    </tr>
                    <tr v-if="organization.contacts.length === 0">
                        <td class="px-6 py-4 border-t" colspan="4">No contacts found.</td>
                    </tr>
                </table>

            </div>
        </div>
    </Layout>
    
</template>